package com.ciwong.epaper.modules.me.adapter;

import com.ciwong.mobilelib.utils.ViewHolder;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.ServicePriceInfo;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * 购买服务Adapter
 *
 * @author bin
 * @version ciwong v.1.0 2015/8/10 17:33
 * @since ciwong v.1.0
 */
public class BuyServiceAdapter extends BaseAdapter
{
    /**
     * 上下文对象
     */
    private Context mContext;

    /**
     * 服务售价信息集合
     */
    private List<ServicePriceInfo> mServicePriceInfos;

    /**
     * 是否购买过
     */
    private boolean isBuy;

    /**
     * 构造函数
     * 
     * @param context
     *            上下文对象
     * @param servicePriceInfos
     *            服务售价信息集合
     * @param isBuy
     *            是否购买过
     */
    public BuyServiceAdapter(Context context,
            List<ServicePriceInfo> servicePriceInfos, boolean isBuy)
    {
        this.mContext = context;
        this.mServicePriceInfos = servicePriceInfos;
        this.isBuy = isBuy;
    }

    @Override
    public int getCount( )
    {
        return mServicePriceInfos == null ? 0 : mServicePriceInfos.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mServicePriceInfos == null || mServicePriceInfos.isEmpty() ? null
                : mServicePriceInfos.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View v;
        if (convertView == null)
        {
            v = View.inflate(mContext, R.layout.adapter_buy_service, null);
        }
        else
        {
            v = convertView;
        }
        TextView tvServer = ViewHolder.get(v, R.id.buy_service_tv);
        TextView discountTv = ViewHolder.get(v, R.id.buy_service_discount_tv);
        TextView originalPriceTv = ViewHolder.get(v,
                R.id.buy_service_original_price_tv);
        TextView isOpenUp = ViewHolder.get(v,
                R.id.buy_service_opening_status_tv);

        ServicePriceInfo servicePriceInfo = (ServicePriceInfo) getItem(
                position);
        tvServer.setText(mContext.getString(R.string.server_tv_show,
                servicePriceInfo.getMonthcount(), servicePriceInfo.getPrice()));

        // 原始价格
        if (servicePriceInfo.getOriginalPrice() > 0)
        {
            originalPriceTv.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            originalPriceTv.setVisibility(View.VISIBLE);
            originalPriceTv.setText(servicePriceInfo.getOriginalPrice() + "元");
        }
        else
        {
            originalPriceTv.setVisibility(View.GONE);
        }
        // 折扣信息
        if (servicePriceInfo.getDiscount() == 10)
        {
            discountTv.setVisibility(View.GONE);
            originalPriceTv.setVisibility(View.GONE);
        }
        else
        {
            discountTv.setVisibility(View.VISIBLE);
            originalPriceTv.setVisibility(View.VISIBLE);
            discountTv.setText(servicePriceInfo.getDiscount() + "折");
        }
        // 开通/续费
        if (isBuy)
        {
            isOpenUp.setText(mContext.getResources().getString(R.string.renew));
        }
        else
        {
            isOpenUp.setText(
                    mContext.getResources().getString(R.string.dredge));
        }
        return v;
    }

    /**
     * 清空数据
     */
    public void clear()
    {
        if (mServicePriceInfos != null)
        {
            mServicePriceInfos.clear();
            notifyDataSetChanged();
        }
    }

    /**
     * 添加数据集合到列表
     * 
     * @param servicePriceInfos
     *            数据集合
     */
    public void addAll(List<ServicePriceInfo> servicePriceInfos)
    {
        if (servicePriceInfos != null)
        {
            if (mServicePriceInfos == null)
            {
                mServicePriceInfos = servicePriceInfos;
            }
            else
            {
                mServicePriceInfos.addAll(servicePriceInfos);
            }
            notifyDataSetChanged();
        }
    }
}
